package com.htjx.mall.services.wallet;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.htjx.mall.commons.mappers.TicketMapper;
import com.htjx.mall.commons.models.User;
import com.htjx.mall.commons.models.enums.SeqType;
import com.htjx.mall.commons.models.user.Ticket;
import com.htjx.mall.services.SequenceManager;

@Service
public class TicketService {
	@Autowired
	private TicketMapper ticketMapper;
	@Autowired
	private SequenceManager sequenceManager;
	@Autowired
	private TransactionTemplate transactionTemplate;
	
	public void insertBatch(final List<Ticket> ticketList) {
		final int size = ticketList.size();
		long[] seq = sequenceManager.buildBatchSeq(SeqType.Ticket, size);
		for(int i = 0; i < size; i ++) {
			ticketList.get(i).setId(seq[i]);
		}
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				ticketMapper.insertBatch(ticketList);
			}
		});
	}
	
	public List<Ticket> selectTickets(User user, Date date) {
		return ticketMapper.selectBatch(user.getId(), date);
	}
}
